/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Chat;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author Mr Bao
 */
public class Util {

    public static Font FontSmall = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_SMALL);
    public static Font fontMedium = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
    public static Font fontChat = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
    public static Font fontLinkWeb = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_ITALIC, Font.SIZE_SMALL);
    public static int color1 = makeColorAsInt(0, 225, 225, 225);
    public static int color2 = makeColorAsInt(0, 58, 90, 255);
    public static int color3 = makeColorAsInt(0, 0, 0, 220);
    public static int color21 = makeColorAsInt(0, 72, 118, 255);
    public static int color11 = makeColorAsInt(0, 255, 255, 255);
    public static int textColor = makeColorAsInt(0, 40, 40, 40);
    public static int colorBlue = makeColorAsInt(0, 58, 90, 255);
    public static int colorGreen = makeColorAsInt(0, 145, 44, 238);
    public static int colorPink = makeColorAsInt(0, 255, 105, 180);
    public static int colorBlue1 = makeColorAsInt(0, 72, 118, 255);
    public static int colorGreen1 = makeColorAsInt(0, 125, 34, 218);
    public static int colorPink1 = makeColorAsInt(0, 240, 128, 128);
    public static int colorBlue3 = makeColorAsInt(0, 0, 0, 220);
    public static int colorGreen3 = makeColorAsInt(0, 125, 38, 205);
    public static int colorPink3 = makeColorAsInt(0, 205, 96, 144);
    public static int[] colors = {colorBlue, colorGreen, colorPink};
    public static int[] colors1 = {colorBlue1, colorGreen1, colorPink1};
    public static int[] colors3 = {colorBlue3, colorGreen3, colorPink3};
    public static final int VERTICAL = 0;
    public static final int HORIZONTAL = 1;

    public static void ClearScreen(Graphics g, int width, int height) {
        g.setColor(255, 255, 255);
        g.fillRect(0, 0, width, height);
    }

    public static void GradientBox(Graphics g, int color1, int color2, int left, int top, int width, int height, int orientation) {
        int max = orientation == VERTICAL ? height : width;

        for (int i = 0; i < max; i++) {
            int color = midColor(color1, color2, max * (max - 1 - i) / (max - 1), max);

            g.setColor(color);

            if (orientation == VERTICAL) {
                g.drawLine(left, top + i, left + width - 1, top + i);
            } else {
                g.drawLine(left + i, top, left + i, top + height - 1);
            }
        }
    }

    public static void DrawTitleBar(Graphics g, String left, String center, String right, int width, int height) {
        GradientBox(g, color1, color2, 0, 0, width, fontMedium.getHeight() / 2, 0);
        g.setColor(color3);
        g.fillRect(0, fontMedium.getHeight() / 2, width, fontMedium.getHeight() / 2);
        g.setColor(210, 210, 210);

        if (left != null) {
            g.setFont(FontSmall);
            g.drawString(left, width / 40 + 6, FontSmall.getHeight() / 8, 0);
        }

        if (center != null) {
            g.setFont(fontMedium);
            g.drawString(center, width * 39 / 80 - fontMedium.stringWidth(center) / 2, -2, 0);
        }

        if (right != null) {
            g.setFont(FontSmall);
            g.drawString(right, width * 39 / 40 - FontSmall.stringWidth(right), FontSmall.getHeight() / 8, 0);
        }
    }

    public static void DrawBottomBar(Graphics g, String left, String center, String right, int width, int height) {
        GradientBox(g, color1, color2, 0, height - fontMedium.getHeight(), width, fontMedium.getHeight(), 0);
        g.setColor(color3);
        g.fillRect(0, height - fontMedium.getHeight() / 2, width, fontMedium.getHeight() / 2);
        g.setColor(210, 210, 210);
        g.setFont(FontSmall);

        if (left != null) {
            g.drawString(left, width / 40, height - FontSmall.getHeight() * 9 / 8, 0);
        }

        if (center != null) {
            g.drawString(center, width * 39 / 80 - FontSmall.stringWidth(center) / 2, height - FontSmall.getHeight() * 9 / 8, 0);
        }

        if (right != null) {
            g.drawString(right, width * 39 / 40 - FontSmall.stringWidth(right), height - FontSmall.getHeight() * 9 / 8, 0);
        }
    }

    static int midColor(int color1, int color2, int prop, int max) {
        int red =
                (((color1 >> 16) & 0xff) * prop + ((color2 >> 16) & 0xff) * (max - prop)) / max;

        int green =
                (((color1 >> 8) & 0xff) * prop + ((color2 >> 8) & 0xff) * (max - prop)) / max;

        int blue =
                (((color1 >> 0) & 0xff) * prop + ((color2 >> 0) & 0xff) * (max - prop)) / max;

        int color = red << 16 | green << 8 | blue;

        return color;
    }

    public static int makeColorAsInt(int alpha, int red, int green, int blue) {
        return alpha << 24 | red << 16 | green << 8 | blue;
    }
}
